<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\UserImRepository;
use App\Repository\Models\UserIm;

/**
 * Class UserImRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class UserImRepositoryEloquent extends BaseRepository implements UserImRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'name',
        'user.name',
        'user.phone',
        'user.shop_id',
    ];

    /**
     * 将前端字段名替换为关联查询模式
     * @var array
     */
    public $fieldSearchReplace = [
        'user_name' => 'user.name',
        'phone' => 'user.phone',
        'shop_id' => 'user.shop_id',
    ];
    /**
     * 可关联查询的字段
     */
    public $withAllow = [
        'user',
    ];
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return UserIm::class;
    }

    /**
     *  检测该用户是否有 IM 账户
     *  @var int user_id
     *  @return boolean
     * */
    public function userImCheck($user_id){
        if (!isset($user_id)||$user_id==null||$user_id==''){
            return false;
        }else {
            $ImInfo = UserIm::query()->where(['user_id'=>$user_id])->first();
            if ($ImInfo==NULL){
                return false;
            }else{
                $clouse = $ImInfo->toArray();
                if (!isset($clouse['id'])){
                    return false;
                }
            }
        }
        return true;
    }
}
